<?php

namespace common\models;

use Yii;
use yii\db\ActiveRecord;
use yii\helpers\Json;

/**
 * 基础 Model
 */
class BaseModel extends ActiveRecord
{
    /**
     * 根据条件查询多条数据, 并按某个字段索引数据
     * @param string $index_by 索引字段
     * @param array $condition 搜索条件 如：$condition = ['and'];$condition[] = ['=','id',1];$condition[] = ['LIKE','name','dkh'];
     * @param array $select  排获取字段
     * @param bool $as_array 返回数组
     * @return array|ActiveRecord[]
     * * */
    public static function getIndexAll($index_by, $condition = [], $select = [], $as_array = true)
    {
        return self::getAll($condition, $select, '', $index_by, $as_array);
    }

    /**
     * 根据条件查询多条数据, 并按某个字段分组数据
     * @param string $group_by 分组字段
     * @param array $condition 搜索条件 如：$condition = ['and'];$condition[] = ['=','id',1];$condition[] = ['LIKE','name','dkh'];
     * @param array $select  排获取字段
     * @param bool $as_array 返回数组
     * @return array|ActiveRecord[]
     * * */
    public static function getGroupAll($group_by, $condition = [], $select = [], $as_array = true)
    {
        $data = self::getAll($condition, $select, '', '', $as_array);

        $res = [];
        foreach ($data as $item) {
            $res[$item[$group_by]][] = $item;
        }

        return $res;
    }

    /**
     * 根据条件查询多条数据
     * @param array $condition 搜索条件 如：$condition = ['and'];$condition[] = ['=','id',1];$condition[] = ['LIKE','name','dkh'];
     * @param array $select  排获取字段
     * @param string $order_by 排序
     * @param string $index_by 索引字段 
     * @param bool $as_array 返回数组
     * @return array|ActiveRecord[]
     * * */
    public static function getAll($condition = [], $select = [], $order_by = '', $index_by = '', $as_array = true)
    {
        $query = self::find();

        if ($condition) $query->where($condition);
        if ($select) $query->select($select);
        if ($order_by) $query->orderBy($order_by);
        if ($index_by) $query = $query->indexBy($index_by);
        if ($as_array) $query = $query->asArray();
        return $query->all();
    }

    /**
     * 获取单条数据
     * @param array $condition 搜索条件 如：$condition = ['and'];$condition[] = ['=','id',1];$condition[] = ['LIKE','name','dkh'];
     * @param string $order_by 排序 
     * @param array $select  排获取字段
     * @return array
     * * */
    public static function getDataOne($condition = [], $order_by = 'id DESC', $select = [])
    {
        $query = self::find();

        if ($select) {
            $query->select($select);
        }
        if ($condition) {
            $query->where($condition);
        }
        if ($order_by) $query->orderBy($order_by);
        
        return $query->asArray()->one();
    }
    
    /**
     * 获取多条数据（分页）
     * @param array $condition [AND,['=','id',1],['','',''],...] 搜索条件
     * @param int $page 页数 
     * @param int $page_size 每页总数 
     * @param string $order_by 排序 
     * @param array $select  排获取字段
     * @return array
     * * */
    public static function getDataAll($condition = [], $page = 1, $page_size = 20, $order_by = 'id DESC', $select = [])
    {
        $query = self::find()->select($select);
        list($offset, $limit) = \common\tools\Util::getLimit($page, $page_size);
        if ($condition) {
            $query->where($condition);
        }
        $data['total'] = intval($query->count());
        $query->offset($offset)->limit($limit)->orderBy($order_by);
        // echo $query->createCommand()->getRawSql();
        $data['list'] = $query->asArray()->all();
        return $data;
    }

    /**
     * 新增/编辑
     * @param array $params
     * * */
    public static function create($params)
    {
        if (!is_array($params)) {
            return false;
        }
        
        $model = new static();
        $model->setAttributes($params, false);
        
        if($model->save()) {
            return $model->id;
        }
        return false;
        
    }
    
    /**
     * 根据条件更新对应的字段
     * @param array $params
     * @param array $condition
     * * */
    public static function updateData($params, $condition)
    {
        if (empty($condition) || empty($params)) {
            return false;
        }
        if (!is_array($condition) || !is_array($params)) {
            return false;
        }
        if (empty(self::find()->where($condition)->all())) {
            return false;
        }

        return self::updateAll($params, $condition);
    }

    /**
     * 根据条件删除
     * @param array $condition
     * * */
    public static function del($condition)
    {
        if (empty($condition)) {
            return false;
        }
        if (empty(self::find()->where($condition)->all())) {
            return false;
        }
        return self::deleteAll($condition);
    }

    // 软删除
    public static function softDel($condition)
    {
        if (empty($condition)) {
            return false;
        }
        if (!is_array($condition)) {
            return false;
        }
        if (empty(self::find()->where($condition)->all())) {
            return false;
        }

        return self::updateAll(['is_del'=> 2], $condition);
    }

    /**
     * 设置Json数据
     * @param  array  $data     数据
     * @param  string $field    字段名
     */
    public function setJson($data, $field)
    {
        $data = array_merge($this->getJson($field), $data);
        
        $this[$field] = Json::encode($data);
    }

    /**
     * 获取Json数据
     * @param  string $field    字段名
     * @return array
     */
    public function getJson($field)
    {
        return $this[$field] ? Json::decode($this[$field]) : [];
    }
}
